library(tidyverse)
library(haven)
library(reshape2)
library(estimatr)
library(hrbrthemes)
library(extrafont)
library(sjPlot)

setwd("")
load("diffindiff.RData")
dat <- read_dta("master-for-replication.dta")

# Figure S10 --------------------------------------------------------------


dnd <- t1 %>% group_by(fn) %>% summarize(hn1 = mean(num_hn_t1)/21,
                                         hn2 = mean(num_hn_t2)/39)

dnd_gg <- melt(dnd, measure.vars = 2:3, value.name = "hn", variable.name = "year")
dnd_gg$fn <- factor(dnd_gg$fn, labels = c("no", "yes"))
levels(dnd_gg$year) <- c("2015", "2016")

g <- ggplot(dnd_gg, aes(year, hn, color = fn)) + geom_point() + 
      geom_line(aes(year, hn, group = fn)) + ylim(0, 20) +
      ylab("Average hard news articles visited per day") + xlab("") +
      theme(legend.position = "bottom", 
            axis.title.x = element_blank()) +
      scale_color_grey(name = " Consumed untrustworthy\n news in 2016?", start = 0.5, end = .01) 

dnew <- g + theme_ipsum_rc()
dnew

# ggsave("hn_dnd.png", dnew, width = 8)


# Table S12 ---------------------------------------------------------------


t12 <- melt(t1, id.vars = "caseid", measure.vars = c("num_hn_t1", "num_hn_t2"), value.name = "num_hn", 
               variable.name = "year")
t12 <- left_join(t12, select(dat, caseid, totalvisits, totalfakecount_grinberg, totalfakebinary_grinberg))
t12 <- inner_join(t12, select(dat, caseid, voteNovember, knowledge:polinterest))

dnd.model.1 <- lm_robust(num_hn ~ totalfakebinary_grinberg * year, data = subset(t12, voteNovember <= 2), clusters = caseid)
dnd.model.2 <- lm_robust(num_hn ~ totalfakebinary_grinberg * year + trumpsupport + knowledge + college + female +
                  nonwhite + age3044 + age4559 + age60plus + polinterest, data = subset(t12, voteNovember <= 2), clusters = caseid)
dnd.model.3 <- lm_robust(num_hn ~ totalfakebinary_grinberg * year + trumpsupport + knowledge + college + female +
                  nonwhite + age3044 + age4559 + age60plus + polinterest + totalvisits, data = subset(t12, voteNovember <= 2), clusters = caseid)

summary(dnd.model.1)
summary(dnd.model.2)
summary(dnd.model.3)

tab_model(dnd.model.1, dnd.model.2, dnd.model.3, 
          show.se = TRUE, ci.hyphen = ", ", col.order = c("est", "se", "p", "ci"), 
          string.est = "b", string.se = "s.e.", string.ci = "95% CI", emph.p = FALSE)

